<?php
/**
* Update logs
* 
*/
    global $db, $user;

    $config = $db->selectObject('rsnewsmodule_config',"location_data='".serialize($loc)."'");
    if (empty($config)) {
        $config->sortorder = 'DESC';
        $config->sortfield = 'posted';
        $config->item_limit = 10;
        $config->enable_pagination = 1;
        $config->aggregate = array();
    }

    $locsql = "(location_data='".serialize($loc)."'";
    if (!empty($config->aggregate)) {
        $locations = unserialize($config->aggregate);
        foreach ($locations as $source) {
            $tmploc = null;
            $tmploc->mod = 'rsnewsmodule';
            $tmploc->src = $source;
            $tmploc->int = '';
            $locsql .= " OR location_data='".serialize($tmploc)."'";
        }
    }
    $locsql .= ')';
    
    // 10/01/2010 - immanuel192 - get module title - begin
    // get module title
    // because if the client switch page, so, that is the action and exponent does not transfer $title and $views        
    $temp=$db->selectObject("container","internal = '" . serialize($loc) . "'");
    if ($temp)
    {
        $title=$temp->title;
        $view=$temp->view;
    }
    else
    {
        $title="";
        $view="Summary"; // default viewer for rsnews module. hehe
    }
    // 10/01/2010 - immanuel192 - get module title - end
    
    // Check permissions for AP link
    $canviewapproval = false;
    if ($user) $canviewapproval = exponent_permissions_check('approve',$loc) || exponent_permissions_check('manage_approval',$loc);
    if (!$canviewapproval) { // still not able to view
        foreach($db->selectObjects('rsnewsitem', $locsql." AND (publish = 0 or publish <= " . time() . ") AND (unpublish = 0 or unpublish > " . time() . ') AND approved != 0') as $post) {
            if ($user && $user->id == $post->poster) {
                $canviewapproval = true;
                break;
            }
        }
    }
    
    $template = new template('rsnewsmodule',$view,$loc);

    $template->assign('moduletitle',$title);
    $template->register_permissions(
        array('administrate','configure','add_item','delete_item','edit_item','manage_approval','view_unpublished'),
        $loc
    );
    
    // pagination by item_limit
    $total = $db->countObjects('rsnewsitem', $locsql." AND (publish = 0 or publish <= " . 
    time() . ") AND (unpublish = 0 or unpublish > " . time() . ") AND approved != 0");
    
    $max_item=$config->item_limit;
    
    // page split
    // cal how many posts
    $page_count=floor($total / $max_item);
    if ($total % $max_item != 0)
        $page_count++;

    // get page
    $page=1;
    if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
    if (($page<1) || ($page>$page_count)) $page=1;
    $start_pos=($page-1)*$max_item;
    
    // pagination
    $rsnews = $db->selectObjects('rsnewsitem',$locsql." AND (publish = 0 or publish <= " . 
     time() . ') AND (unpublish = 0 or unpublish > ' . time() . ') AND approved != 0 ORDER BY '.
        $config->sortfield.' ' . $config->sortorder . $db->limit($config->item_limit,$start_pos));

    foreach ($rsnews as $item){
        // real post must be calculated again to new time zone
        // TODO :::
        
        $item->real_posted =($item->publish != 0 ? $item->publish : $item->posted);
        $nloc = exponent_core_makeLocation($loc->mod,$loc->src,$item->id);
        $item->permissions = array(
            'edit_item'=>((exponent_permissions_check('edit_item',$loc) || exponent_permissions_check('edit_item',$nloc)) ? 1 : 0),
            'delete_item'=>((exponent_permissions_check('delete_item',$loc) || exponent_permissions_check('delete_item',$nloc)) ? 1 : 0),
            'administrate'=>((exponent_permissions_check('administrate',$loc) || exponent_permissions_check('administrate',$nloc)) ? 1 : 0)
        );
        
        // date time
        $date_time=0;
        if ($item->edited == 0)
            $date_time=$item->real_posted;
        else
            $date_time=$item->edited;

        $item->datetime=_ab_dateFormat($date_time);
        // end of date time
        
        // for poster name
        $temp=$db->selectObject("user","id = " . $item->poster);
        $item->poster_name = "";
        if ($temp)
        {
            $item->poster_name = $temp-> firstname ." ". $temp->lastname;
        }                                       

        // for image
        //Get the image file if there is one.
        if (isset($item->file_id) && $item->file_id > 0) {
            $file = $db->selectObject('file', 'id='.$item->file_id);
            $item->image = BASE_URL . "/".$file->directory.'/'.$file->filename;
        }
        //Get the image file if there is one.
        if (isset($item->background_id) && $item->background_id > 0) {
            $file = $db->selectObject('file', 'id='.$item->background_id);
            $item->background_path ="/".$file->directory.'/'.$file->filename;
        }
        else $item->background_path="";
        
    }
    
    switch($config->sortfield) {
        case "posted":
            $field = "Posted";
            break;
        case "publish":
            $field = "Posted";
            break;
        case "edited":
            $field = "Edited";
            break;
        default:
            $field = "Posted";
            break;
    }

    if ($config->sortorder == "ASC") {
        $order = "Ascending";
    } else {
        $order = "Descending";
    }

    $sortFunc = 'exponent_sorting_by'.$field.$order;

    if (!defined('SYS_SORTING')) require_once(BASE.'subsystems/sorting.php');
    usort($rsnews,$sortFunc);
    $template->assign('canview_approval_link',$canviewapproval);

    // pagination
    $template->assign('item_limit', $config->item_limit);
    $template->assign('pages',$page_count);
    $template->assign('page',$page);
    $template->assign('news',$rsnews);
    
    $template->assign('config', $config);
    // Change if clase here, only check that flow_has_set has check or not
    if (!isset($_REQUEST['flow_has_set']))
        exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
    
    // page title    
    $page_title=exponent_lang_getText("rsnews_view_page_title");
    if ($page >1)
    {
        $page_title.=" - ".exponent_lang_getText("page")." ".$page;
    }
    $page_title.=" - ".SITE_TITLE;
    _ab_theme_action_register_title($page_title);
    // Feb 12, 2011 - trungdt - set page title - end
    $template->output();
?>